<!-- 任务执行情况 -->
<template>
  <div>
    <el-dialog title="任务详情" :visible.sync="editFormVisible" :close-on-click-modal="false"
               :close-on-press-escape="false" @close="$emit('update:visible',false)">
      <el-form :model="editForm" label-width="100px" ref="editForm" v-if="editForm">
        <el-form-item label="任务名称:">
          {{editForm.name}}
        </el-form-item>
        <el-form-item label="发布人:">
          {{editForm.admin_name}}
        </el-form-item>
        <el-form-item label="下发时间:">
          {{editForm.create_time}}
        </el-form-item>
        <el-form-item label="限改时间:">
          {{editForm.end_time}}
        </el-form-item>
        <el-form-item label="任务文件:">
          <el-button size="small" v-if="editForm.appendix" @click="priveWord">预览文件</el-button>
          <span v-else>无</span>
        </el-form-item>
        <el-form-item label="任务描述:">
          <div v-html="editForm.descript" class="contentTitle"></div>
        </el-form-item>
        <el-form-item label="任务状态:">
          <div v-if="editForm.is_end == '未处理'">
            <el-button type="primary" @click.native="taskTakeFn" size="small">接受任务</el-button>
            <el-button type="danger" @click.native="taskChangeTakeFn('detail')" size="small">转派任务</el-button>
            <span class="c-danger">（转派指定人员任务）</span>
          </div>
          <div v-else>
            {{editForm.is_end}}
            <el-button type="primary" size="small" class="ml-10"
                       @click="openCommonDialog(3)" v-if="editForm.is_end=='执行中'">
              多次完成
<!--              执行日志-->
            </el-button>
            <el-button type="success" size="small" class="ml-10"
                       @click="openCommonDialog(1)" v-if="editForm.is_end=='执行中'">立即完成
            </el-button>
          </div>
          <div @click="handleEdit(editForm.id)" class="c-danger lookLsit">查看执行情况</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!--<el-button @click.native="resetForm">取消</el-button>-->
        <el-button type="primary" @click.native="editSubmit" size="small">确定</el-button>
      </div>
    </el-dialog>
    <!--detail 任务详情信息 getUser 执行函数 commonType回复类型，1去执行 2管理员评论-->
    <teskCommon :visible.sync="commonvisible" v-if="commonvisible" @getUser="getUser" :detail="editForm"
                :commonType="commonType" :taskUserId="taskUserId"></teskCommon>
    <!--转派人员 userLine 已指派人员 userNumber 选择用户人数-->
<!--    <chooseAloneUser :visible.sync="uservisible" v-if="uservisible" @getUser="getUser"-->
<!--                     :optionArry="editForm"></chooseAloneUser>-->
<!--    <singleTeacherModle></singleTeacherModle>-->
    <singleTeacherModle :visible.sync="uservisible" @studentFn="getUser"></singleTeacherModle>
    <drawerTask @checkTask="checkTask" :visible.sync="dialogDrawerVisible" :taskId="taskId" v-if="dialogDrawerVisible"></drawerTask>
    <div class="iframeBoxBody" v-if="iframeState">
      <img src="@/assets/imageAdmin/closeimg.png" alt="" @click.stop="iframeState=false">
      <webview id="foo" :src="iframeSrc" style="display:inline-flex; width:840px; height:680px;background:#fff;"
               plugins v-if="iframeState"></webview>
      <!--<iframe id="foo" frameborder=0 name="showHere" scrolling=auto :src="url" class="iframeBoxLine" v-if="iframeState"></iframe>-->
    </div>
  </div>
</template>
<script>
  import api from '@/utils/api.js'
  import teskCommon from '@/components/dialog/taskCommonDialog.vue'
  import drawerTask from '@/components/dialog/drawerTask.vue'
  import chooseAloneUser from '@/components/dialog/chooseAloneUser.vue'

  export default {
    name: 'taskDetailDialog',
    props: ['visible', 'editBoxId'],
    components: {
      teskCommon,
      drawerTask,
      chooseAloneUser
    },
    watch: {
      visible () {
        this.editFormVisible = this.visible
        console.log(this.visible)
      }
    },
    data () {
      return {
        iframeState: false,
        iframeSrc: '',
        dialogDrawerVisible: false,
        editFormVisible: false,
        editForm: null,
        commonvisible: false,
        taskId: null,
        taskUserId: null,
        userInfomation: '',
        // 转派
        uservisible: false,
        userNumber: 1 // 选择用户人数 1 单选 2多选
      }
    },
    created () {
      this.editFormVisible = this.visible
      this.db.findOne({
        name: 'userInfomation'
      }, (err, ret) => {
        console.log('获取用户信息内容', ret.value)
        this.userInfomation = ret.value
      })
      this.InitFn()
      this.httpFnLine()
    },
    methods: {
      // 详情数据
      InitFn () {
        const loading = this.$loading({
          lock: true,
          text: '读取中…',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.7)'
        })
        this.$fetch(api.task_edit, {id: this.editBoxId}).then((response) => {
          console.log('数据：', response)
          if (response.code === 0) {
            this.editForm = response.vo
            // _self.$nextTick(function () {
            //   _self.checked('first') // 每次更新了数据，触发这个函数即可。
            // })
            this.DetailListFn(loading)
          } else {
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      DetailListFn (loading) {
        console.log('用户信息:', this.userInfomation)
        this.$fetch(api.task_replylist_id + this.editBoxId, {}).then((response) => {
          if (response.result === 1) {
            for (let i in response.list) {
              // if (response.list[i].doer_id === response.admin_id) {
              if (response.list[i].doer_id === this.userInfomation.id) {
                this.taskUserId = response.list[i].id
              }
            }
            loading.close()
          } else {
          }
        }).catch((err) => {
          console.log(err)
        })
      },
      handleClose () {
        console.log('关闭了')
        this.editFormVisible = false
      },
      handleEdit () {
        this.taskId = this.editForm.id
        this.dialogDrawerVisible = true
      },
      // 接受任务
      taskTakeFn () {
        let _self = this
        this.$confirm('确定接受本次任务?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log('确认接受任务')
          _self.$fetch('task/accept?id=' + this.editForm.id).then((response) => {
            if (response.code === 0) {
              _self.$message({
                type: 'success',
                message: '接受成功!'
              })
              _self.InitFn()
            } else {
            }
          }).catch((err) => {
            console.log(err)
          })
        }).catch(() => {
          console.log('取消')
        })
      },
      // 转派任务面板弹窗
      taskChangeTakeFn (option) {
        this.uservisible = true
      },
      openCommonDialog (option) {
        this.commonType = option
        this.commonvisible = true
      },
      editSubmit () {
        // 验证信息
        this.visible = false
      },
      getUser (option) {
        if (option !== 'comment') {
          this.$confirm('你确认将任务转派给：' + option.realname + '？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 请求转派接口 ,转派人员id userId
            this.$post(api.admin_reply, {users: option.id, id: this.editBoxId}).then((response) => {
              if (response.code === 0) {
                this.InitFn()
                this.$emit('InitFn')
                this.$message({
                  type: 'success',
                  message: '转派成功!'
                })
                this.uservisible = false
              } else {
              }
            }).catch((err) => {
              console.log(err)
            })
          }).catch(() => {
          })
        } else {
          this.InitFn()
          this.$emit('InitFn')
        }
      },
      // 弹窗
      checkTask () {

      },
      // 获取默认域名
      httpFnLine () {
        this.$fetch(this.api.system_setting, {}).then(res => {
          if (res.code === 0) {
            this.httpUrlInfo = res.vo.url
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            })
          }
        })
      },
      priveWord () {
        if (this.httpUrlInfo) {
          this.iframeSrc = 'https://view.officeapps.live.com/op/view.aspx?src=' + this.httpUrlInfo + this.editForm.appendix
          this.iframeState = true
        } else {
          this.$message.error('学校还未设置域名，请联系管理员！')
        }
      }
    }
  }
</script>
